<template>
  <div class="un-eseal">
    <el-upload ref="upload" action="/infra/file/upload" :multiple="false" :show-file-list="false"
      accept="image/jpg" :http-request="submitUpload" :before-upload="beforeUpload"
      class="un-eseal-uploader" :style="imgStyles" :disabled="uploadDisabled"
      :class="{ disabled: uploadDisabled }">
      <el-image v-if="url" :src="url" fit="fill" class="un-eseal-image" @click="handleClick"
        @dblclick="selectFile">
        <div slot="error" class="un-eseal-image-error">
          <i class="el-icon-picture-outline"></i>
        </div>
      </el-image>
      <span v-else class="un-eseal-uploader-icon" @click="handleClick" @dblclick="selectFile">
        <i class="el-icon-plus"></i>
        <span class="un-eseal-empty">请双击上传电子签章</span>
      </span>
    </el-upload>
  </div>
</template>

<script>
import { beforeUpload, submitUpload } from "./const";

export default {
  name: "UnEseal",
  inject: {
    elForm: {
      default: "",
    },
  },

  props: {
    width: {
      type: Number,
      default: 200,
    },
    height: {
      type: Number,
      default: 200,
    },
    right: {
      type: [Number, String],
      default: 80,
    },
    bottom: {
      type: [Number, String],
      default: 0,
    },
    value: String,
  },
  computed: {
    imgStyles() {
      return {
        width: `${this.width}px`,
        height: `${this.height}px`,
        right: `${this.right}px`,
        bottom: `${this.bottom}px`,
      };
    },
    uploadDisabled() {
      return this.disabled || (this.elForm || {}).disabled;
    },
    url() {
      let url = this.value || "";
      return url.includes(process.env.VUE_APP_ENV_URL)
        ? url.replace(process.env.VUE_APP_ENV_URL, window.location.origin)
        : url;
    },
  },
  methods: {
    beforeUpload(file) {
      return beforeUpload(file);
    },
    submitUpload(option) {
      return submitUpload(option).then((res) => {
        if (res.code === 0) {
          this.$emit("input", res.data || "");
        }
      });
    },
    // 阻止默认的点击上传
    handleClick(event) {
      event.preventDefault();

      event.stopPropagation();
    },
    selectFile() {
      this.$refs.upload.$refs["upload-inner"].handleClick();
    },
  },
};
</script>

<style lang="scss">
.un-eseal {
  position: relative;
  line-height: 1.2;
}

.un-eseal-uploader {
  position: absolute;
  z-index: 999;
  color: #8c939d;

  .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    width: 100%;
    height: 100%;

    &:hover {
      border-color: #409eff;
    }
  }

  &.disabled {
    .el-upload {
      cursor: default;

      &:hover {
        border-color: #d9d9d9;
      }
    }
  }
}

.un-eseal-uploader-icon {
  width: 100%;
  height: 100%;
  /* line-height: 178px; */
  text-align: center;

  font-size: 18px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;

  i {
    font-size: 28px;
  }
}

.un-eseal-image {
  height: 100%;
  width: 100%;
}

.un-eseal-image-error {
  width: 100%;
  height: 100%;
  display: inline-block;
}

.un-eseal-empty {
  color: #999;
}
</style>
